﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Worktool
{
    public partial class DeliveryChain : Form
    {
        bool userSicht = false;
        bool filterPurch = false;
        bool filterStorage = false;
        bool filterEngineer = false;
        bool filterBereitstellungHHkleinerLieferterminHH = false;
        bool filterBereitstellungWerftkleinerLieferfaehigkeitHH = false;
        bool filterBestelltAberNichtGeliefert = false;
        bool filterGelieferteWareHH = false;
        bool filtergelieferteWareMW = false;

        public DeliveryChain()
        {
            InitializeComponent();
            this.Text = "Lieferkette: " + Properties.Settings.Default.CurrentDBName; 
            vw_comm_LieferketteTableAdapter.updateMaterialDetailExtendedStoredProc();
            this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
            this.dataGridView1.CellParsing += new System.Windows.Forms.DataGridViewCellParsingEventHandler(this.dataGridView1_CellParsing);
            this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(this.dataGridView1_DataError);
            //this.dataGridView1.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dataGridView1_CellValidating);
            //this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
            
        }

        private void DeliveryChain_Load(object sender, EventArgs e)
        {
            this.vw_comm_Lieferkette_KaiExportTableAdapter.Fill(this.aSCDataSet.vw_comm_Lieferkette_KaiExport);
            this.vw_comm_LieferketteTableAdapter.Fill(this.aSCDataSet.vw_comm_Lieferkette);
            dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
            dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
            
            //dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].ValueType = typeof(DateTime);

        }

        // Filter setzen
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND ISNULL(JLM,'') LIKE '{3}' AND ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND ISNULL(JLM,'') LIKE '{3}' AND ISNULL(Positions_ID,'')  LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND ISNULL(JLM,'') LIKE '{3}' AND ISNULL(Positions_ID,'')  LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND ISNULL(JLM,'') LIKE '{3}' AND ISNULL(Positions_ID,'')  LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND ISNULL(JLM,'') LIKE '{3}' AND ISNULL(Positions_ID,'')  LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
    
        }



        private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            
            if (dataGridView1.CurrentCell.OwningColumn.ValueType == typeof(DateTime))
            {
                DateTime dResult;
                if(DateTime.TryParse(e.Value.ToString(), out dResult))
                {
                    dataGridView1.CurrentCell.Value = dResult;
                }
                else
                {
                    //e.Value = "";
                    //dataGridView1.CancelEdit();
                    dataGridView1.CurrentCell.Value = null;
                    e.ParsingApplied = true;
                }
            }
            else if (dataGridView1.CurrentCell.OwningColumn.ValueType == typeof(String))
            {
                //MessageBox.Show("Typ String");
            }

        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DateTime? dbbestellFreigabe;
            DateTime? dbbestaetigterLiefertermin;
            DateTime? dbbestellung;
            DateTime? dbwareneingang;
            DateTime? dbwareLieferSollHH;
            DateTime? dbwareLieferIstHH;
            DateTime? dblieferungWerftIst;
            DateTime? dblieferungSchiffSoll;



            // Eventhandler anhalten
            this.dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
            
            this.vwcommLieferketteBindingSource1.EndEdit();
            updateUserColumn();
            
            // Die Datumsspalten nach Null checken
            if (dataGridView1.CurrentRow.Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.CurrentRow.Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value == DateTime.MinValue) 
            { dbbestellFreigabe = null; } 
            else 
            { dbbestellFreigabe = (DateTime)dataGridView1.CurrentRow.Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?) dataGridView1.CurrentRow.Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value == DateTime.MinValue ) 
            { dbbestaetigterLiefertermin = null; } 
            else 
            { dbbestaetigterLiefertermin = (DateTime)dataGridView1.CurrentRow.Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?) dataGridView1.CurrentRow.Cells["bestellungDataGridViewTextBoxColumn"].Value == DateTime.MinValue ) 
            { dbbestellung = null; } 
            else 
            { dbbestellung = (DateTime)dataGridView1.CurrentRow.Cells["bestellungDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.CurrentRow.Cells["wareneingangDataGridViewTextBoxColumn"].Value == DateTime.MinValue) 
            { dbwareneingang = null; } 
            else 
            { dbwareneingang = (DateTime)dataGridView1.CurrentRow.Cells["wareneingangDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.CurrentRow.Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue ) 
            { dbwareLieferSollHH = null; } 
            else 
            { dbwareLieferSollHH = (DateTime)dataGridView1.CurrentRow.Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value; }
           
            if (dataGridView1.CurrentRow.Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.CurrentRow.Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue ) 
            { dbwareLieferIstHH = null; } 
            else 
            { dbwareLieferIstHH = (DateTime)dataGridView1.CurrentRow.Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?) dataGridView1.CurrentRow.Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value == DateTime.MinValue ) 
            { dblieferungWerftIst = null; } 
            else 
            { dblieferungWerftIst = (DateTime)dataGridView1.CurrentRow.Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value; }
            
            if (dataGridView1.CurrentRow.Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.CurrentRow.Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value == DateTime.MinValue) 
            { dblieferungSchiffSoll = null; } 
            else 
            { dblieferungSchiffSoll = (DateTime)dataGridView1.CurrentRow.Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value; }

            
            // Daten wieder zurück in die DB schreiben
            this.vw_comm_LieferketteTableAdapter.updateMaterialDetailExtended(
                dataGridView1.CurrentRow.Cells["materialStatusDataGridViewTextBoxColumn"].Value.ToString(),
                dbbestellFreigabe,
                dataGridView1.CurrentRow.Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value.ToString(),
                dbbestellung,
                dataGridView1.CurrentRow.Cells["userBestellungDataGridViewTextBoxColumn"].Value.ToString(),
                dbbestaetigterLiefertermin,
                dataGridView1.CurrentRow.Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString(),
                dbwareneingang,
                dataGridView1.CurrentRow.Cells["userWareneingangDataGridViewTextBoxColumn"].Value.ToString(),
                dbwareLieferSollHH,
                dataGridView1.CurrentRow.Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString(),
                dbwareLieferIstHH,
                dataGridView1.CurrentRow.Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString(),
                dblieferungWerftIst,
                dataGridView1.CurrentRow.Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString(),
                dblieferungSchiffSoll,
                dataGridView1.CurrentRow.Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["versandArtWerftDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["packstueckDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["lieferscheinNummerDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["laufendeNummerAuftragDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["auftragsNummerDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["remarksDataGridViewTextBoxColumn"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["empfangsStelleWerft"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["lagerOrtWerft"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["auftragsJahr"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["kostenStelleMW"].Value.ToString(),
                dataGridView1.CurrentRow.Cells["bedarfMW"].Value.ToString(),
                Convert.ToBoolean(dataGridView1.CurrentRow.Cells["istVeredelungsArtikel"].Value),
                Convert.ToBoolean(dataGridView1.CurrentRow.Cells["istVeredelt"].Value),
                dataGridView1.CurrentRow.Cells["veredelungsSchritt"].Value.ToString(),
                Convert.ToInt32(dataGridView1.CurrentRow.Cells["iDMaterialDetailExtendedDataGridViewTextBoxColumn"].Value.ToString()));
                
            // Eventhandler wieder einschalten
            this.dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
            
        }

        //
        // Filter alle Userspalten
        //
        private void button1_Click(object sender, EventArgs e)
        {
            if (userSicht)
            {
                dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                userSicht = !userSicht;
            }
            else
            {
                dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                userSicht = !userSicht;
            }
        }

        //
        // Falsche Eingaben abfangen
        //
        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {
            // MessageBox.Show("Dieser Wert ist für diese Spalte nicht erlaubt!" /*+ anError.Context.ToString()*/);

            //if (anError.Context == DataGridViewDataErrorContexts.Commit)
            //{
            //    MessageBox.Show("Commit error");
            //}
            //if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
            //{
            //    MessageBox.Show("Cell change");
            //}
            //if (anError.Context == DataGridViewDataErrorContexts.Parsing)
            //{
            //    MessageBox.Show("parsing error");
            //}
            //if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
            //{
            //    MessageBox.Show("leave control error");
            //}

            //if ((anError.Exception) is ConstraintException)
            //{
            //    DataGridView view = (DataGridView)sender;
            //    view.Rows[anError.RowIndex].ErrorText = "an error";
            //    view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

            //    anError.ThrowException = false;
            //}
        }

        //
        // Zellen nach Datum formatieren
        //
        private static void ShortFormDateFormat(DataGridViewCell formatting)
        {
            

            //if (formatting.Value != null && formatting.Value.ToString() != "")
            //{
            //    try
            //    {
            //        DateTime theDate = DateTime.Parse(formatting.Value.ToString());
            //        String dateString = theDate.ToString("dd.MM.yyyy");
            //        formatting.Value = Convert.ToDateTime(dateString);
            //    }
            //    catch (Exception)
            //    {
            //        MessageBox.Show("Nur gültige Daten!");
            //    }
            //}
            //else
            //{
            //    formatting.Value = "01.01.1900";
            //}
        }

        //
        // Copy to Clipbord
        //
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 1 && dataGridView1.SelectedCells[0].Value.ToString() != "")
            {
                Clipboard.SetText(dataGridView1.SelectedCells[0].Value.ToString());
            }
        }

        //
        // Paste from Clipbord
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime? dbbestellFreigabe;
            DateTime? dbbestaetigterLiefertermin;
            DateTime? dbbestellung;
            DateTime? dbwareneingang;
            DateTime? dbwareLieferSollHH;
            DateTime? dbwareLieferIstHH;
            DateTime? dblieferungWerftIst;
            DateTime? dblieferungSchiffSoll;

            
            List<int> pasteZeilen = new List<int> { };
            string pasteColumn = "";
            if (dataGridView1.SelectedCells.Count != 0)
            {
                pasteColumn = dataGridView1.SelectedCells[0].OwningColumn.Name;
            }
            for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            {
                pasteZeilen.Add(dataGridView1.SelectedCells[i].OwningRow.Index);
            }
            if (Clipboard.GetText() != "")
            {
                // Eventhandler anhalten
                this.dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
            
                for (int i = 0; i < pasteZeilen.Count; i++)
                {
                    try
                    {
                        dataGridView1.Rows[pasteZeilen[i]].Cells[pasteColumn].Value = Clipboard.GetText();
                        updateUserColumn();

                        // Die Datumsspalten nach Null checken
                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbbestellFreigabe = null; }
                        else
                        { dbbestellFreigabe = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbbestaetigterLiefertermin = null; }
                        else
                        { dbbestaetigterLiefertermin = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbbestellung = null; }
                        else
                        { dbbestellung = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbwareneingang = null; }
                        else
                        { dbwareneingang = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbwareLieferSollHH = null; }
                        else
                        { dbwareLieferSollHH = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dbwareLieferIstHH = null; }
                        else
                        { dbwareLieferIstHH = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dblieferungWerftIst = null; }
                        else
                        { dblieferungWerftIst = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value; }

                        if (dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                        { dblieferungSchiffSoll = null; }
                        else
                        { dblieferungSchiffSoll = (DateTime)dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value; }

                        
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
                        //if (dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "") { dataGridView1.Rows[pasteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value = "01.01.1900"; }
            
                        // Zeile zurück in die DB schreiben
                        this.vwcommLieferketteBindingSource1.EndEdit();                        
                        this.vw_comm_LieferketteTableAdapter.updateMaterialDetailExtended(
                            dataGridView1.Rows[pasteZeilen[i]].Cells["materialStatusDataGridViewTextBoxColumn"].Value.ToString(),
                            dbbestellFreigabe,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value.ToString(),
                            dbbestellung,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userBestellungDataGridViewTextBoxColumn"].Value.ToString(),
                            dbbestaetigterLiefertermin,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString(),
                            dbwareneingang,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userWareneingangDataGridViewTextBoxColumn"].Value.ToString(),
                            dbwareLieferSollHH,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString(),
                            dbwareLieferIstHH,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString(),
                            dblieferungWerftIst,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString(),
                            dblieferungSchiffSoll,
                            dataGridView1.Rows[pasteZeilen[i]].Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["versandArtWerftDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["packstueckDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["lieferscheinNummerDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["laufendeNummerAuftragDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["auftragsNummerDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["remarksDataGridViewTextBoxColumn"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["empfangsStelleWerft"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["lagerOrtWerft"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["auftragsJahr"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["kostenStelleMW"].Value.ToString(),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["bedarfMW"].Value.ToString(),
                            Convert.ToBoolean(dataGridView1.Rows[pasteZeilen[i]].Cells["istVeredelungsArtikel"].Value),
                            Convert.ToBoolean(dataGridView1.Rows[pasteZeilen[i]].Cells["istVeredelt"].Value),
                            dataGridView1.Rows[pasteZeilen[i]].Cells["veredelungsSchritt"].Value.ToString(),
                            Convert.ToInt32(dataGridView1.Rows[pasteZeilen[i]].Cells["iDMaterialDetailExtendedDataGridViewTextBoxColumn"].Value.ToString()));
                     
                    }
                    catch (Exception ex)
                    { }
                }

                // Eventhandler wieder einschalten
                this.dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
            
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime? dbbestellFreigabe;
            DateTime? dbbestaetigterLiefertermin;
            DateTime? dbbestellung;
            DateTime? dbwareneingang;
            DateTime? dbwareLieferSollHH;
            DateTime? dbwareLieferIstHH;
            DateTime? dblieferungWerftIst;
            DateTime? dblieferungSchiffSoll;
            
            List<int> deleteZeilen = new List<int> { };
            string deleteColumn = "";
            if (dataGridView1.SelectedCells.Count != 0)
            {
                deleteColumn = dataGridView1.SelectedCells[0].OwningColumn.Name;
            }
            for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            {
                deleteZeilen.Add(dataGridView1.SelectedCells[i].OwningRow.Index);
            }
            if (dataGridView1.SelectedCells.Count != 0)
            {
                // Eventhandler anhalten
                this.dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
            
                for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
                {
                    try
                    {
                        if (dataGridView1.SelectedCells[i].ValueType == typeof(System.DateTime))
                        {
                            dataGridView1.SelectedCells[i].Value = DBNull.Value; 
                            updateUserColumn();
                            // Zeile zurück in die DB schreiben
                            this.vwcommLieferketteBindingSource1.EndEdit();
                            
                            // Die Datumsspalten nach Null checken
                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellFreigabe = null; }
                            else
                            { dbbestellFreigabe = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestaetigterLiefertermin = null; }
                            else
                            { dbbestaetigterLiefertermin = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellung = null; }
                            else
                            { dbbestellung = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareneingang = null; }
                            else
                            { dbwareneingang = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferSollHH = null; }
                            else
                            { dbwareLieferSollHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferIstHH = null; }
                            else
                            { dbwareLieferIstHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungWerftIst = null; }
                            else
                            { dblieferungWerftIst = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungSchiffSoll = null; }
                            else
                            { dblieferungSchiffSoll = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value; }

                            
                            this.vw_comm_LieferketteTableAdapter.updateMaterialDetailExtended(
                                dataGridView1.Rows[deleteZeilen[i]].Cells["materialStatusDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellFreigabe,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellung,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellungDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestaetigterLiefertermin,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareneingang,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareneingangDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferSollHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferIstHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungWerftIst,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungSchiffSoll,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["versandArtWerftDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["packstueckDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lieferscheinNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["laufendeNummerAuftragDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["remarksDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["empfangsStelleWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lagerOrtWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsJahr"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["kostenStelleMW"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["bedarfMW"].Value.ToString(),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelungsArtikel"].Value),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelt"].Value),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["veredelungsSchritt"].Value.ToString(),
                                Convert.ToInt32(dataGridView1.Rows[deleteZeilen[i]].Cells["iDMaterialDetailExtendedDataGridViewTextBoxColumn"].Value.ToString()));
                        }
                        if (dataGridView1.SelectedCells[i].ValueType == typeof(int))
                        {
                            dataGridView1.SelectedCells[i].Value = 0;
                            updateUserColumn();
                            // Zeile zurück in die DB schreiben
                            this.vwcommLieferketteBindingSource1.EndEdit();

                            // Die Datumsspalten nach Null checken
                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellFreigabe = null; }
                            else
                            { dbbestellFreigabe = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestaetigterLiefertermin = null; }
                            else
                            { dbbestaetigterLiefertermin = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellung = null; }
                            else
                            { dbbestellung = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareneingang = null; }
                            else
                            { dbwareneingang = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferSollHH = null; }
                            else
                            { dbwareLieferSollHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferIstHH = null; }
                            else
                            { dbwareLieferIstHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungWerftIst = null; }
                            else
                            { dblieferungWerftIst = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungSchiffSoll = null; }
                            else
                            { dblieferungSchiffSoll = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value; }


                            this.vw_comm_LieferketteTableAdapter.updateMaterialDetailExtended(
                                dataGridView1.Rows[deleteZeilen[i]].Cells["materialStatusDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellFreigabe,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellung,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellungDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestaetigterLiefertermin,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareneingang,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareneingangDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferSollHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferIstHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungWerftIst,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungSchiffSoll,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["versandArtWerftDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["packstueckDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lieferscheinNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["laufendeNummerAuftragDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["remarksDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["empfangsStelleWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lagerOrtWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsJahr"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["kostenStelleMW"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["bedarfMW"].Value.ToString(),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelungsArtikel"].Value),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelt"].Value),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["veredelungsSchritt"].Value.ToString(),
                                Convert.ToInt32(dataGridView1.Rows[deleteZeilen[i]].Cells["iDMaterialDetailExtendedDataGridViewTextBoxColumn"].Value.ToString()));
                        }
                        if (dataGridView1.SelectedCells[i].ValueType == typeof(string))
                        {
                            dataGridView1.SelectedCells[i].Value = String.Empty;
                            updateUserColumn();
                            // Zeile zurück in die DB schreiben
                            // Zeile zurück in die DB schreiben
                            this.vwcommLieferketteBindingSource1.EndEdit();

                            // Die Datumsspalten nach Null checken
                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellFreigabe = null; }
                            else
                            { dbbestellFreigabe = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellFreigabeDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestaetigterLiefertermin = null; }
                            else
                            { dbbestaetigterLiefertermin = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestaetigterLieferterminDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbbestellung = null; }
                            else
                            { dbbestellung = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["bestellungDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareneingang = null; }
                            else
                            { dbwareneingang = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareneingangDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferSollHH = null; }
                            else
                            { dbwareLieferSollHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferSollHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dbwareLieferIstHH = null; }
                            else
                            { dbwareLieferIstHH = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["wareLieferIstHHDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungWerftIst = null; }
                            else
                            { dblieferungWerftIst = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungWerftIstDataGridViewTextBoxColumn"].Value; }

                            if (dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString() == "" || (DateTime?)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value == DateTime.MinValue)
                            { dblieferungSchiffSoll = null; }
                            else
                            { dblieferungSchiffSoll = (DateTime)dataGridView1.Rows[deleteZeilen[i]].Cells["lieferungSchiffSollDataGridViewTextBoxColumn"].Value; }


                            this.vw_comm_LieferketteTableAdapter.updateMaterialDetailExtended(
                                dataGridView1.Rows[deleteZeilen[i]].Cells["materialStatusDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellFreigabe,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestellung,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userBestellungDataGridViewTextBoxColumn"].Value.ToString(),
                                dbbestaetigterLiefertermin,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareneingang,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareneingangDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferSollHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dbwareLieferIstHH,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungWerftIst,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value.ToString(),
                                dblieferungSchiffSoll,
                                dataGridView1.Rows[deleteZeilen[i]].Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["versandArtWerftDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["packstueckDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lieferscheinNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["laufendeNummerAuftragDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsNummerDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["remarksDataGridViewTextBoxColumn"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["empfangsStelleWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["lagerOrtWerft"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["auftragsJahr"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["kostenStelleMW"].Value.ToString(),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["bedarfMW"].Value.ToString(),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelungsArtikel"].Value),
                                Convert.ToBoolean(dataGridView1.Rows[deleteZeilen[i]].Cells["istVeredelt"].Value),
                                dataGridView1.Rows[deleteZeilen[i]].Cells["veredelungsSchritt"].Value.ToString(),
                                Convert.ToInt32(dataGridView1.Rows[deleteZeilen[i]].Cells["iDMaterialDetailExtendedDataGridViewTextBoxColumn"].Value.ToString()));
                        }
                    }
                    catch (Exception ex)
                    { }
                }

                // Eventhandler wieder einschalten
                this.dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
            
            }
        }

        private void updateUserColumn() 
        {
            
            // Das entsprechende Userfeld füllen
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userBestellFreigabeDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);
            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userBestellungDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userWareneingangDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userWareLieferSollHHDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userWareLieferIstHHDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userlieferungWerftIstDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Index)
            {
                ShortFormDateFormat(dataGridView1.CurrentCell);
                dataGridView1.CurrentRow.Cells["userLieferungSchiffSollDataGridViewTextBoxColumn"].Value = (Properties.Settings.Default.User.ToString() + " - " + System.DateTime.Now);

            }
        }

        //
        // Filter alle Purchasingspalten
        //
        private void button4_Click(object sender, EventArgs e)
        {
            if (filterPurch)
            {
                
                //dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = true;
                //dataGridView1.Columns["lagerOrtWerft"].Visible = true;
                dataGridView1.Columns["auftragsJahr"].Visible = true;
                dataGridView1.Columns["kostenStelleMW"].Visible = true;
                dataGridView1.Columns["bedarfMW"].Visible = true;
                button4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
                filterPurch = !filterPurch;
            }
            else
            {
                //dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = false;
                //dataGridView1.Columns["lagerOrtWerft"].Visible = false;
                dataGridView1.Columns["auftragsJahr"].Visible = false;
                dataGridView1.Columns["kostenStelleMW"].Visible = false;
                dataGridView1.Columns["bedarfMW"].Visible = false;
                button4.BackColor = Color.Red;
                filterPurch = !filterPurch;
            }
        }

        //
        // Filter alle Storagespalten
        //
        private void button3_Click(object sender, EventArgs e)
        {
            if (filterStorage)
            {

                //dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lagerOrtWerft"].Visible = true;
                //dataGridView1.Columns["auftragsJahr"].Visible = true;
                //dataGridView1.Columns["kostenStelleMW"].Visible = true;
                //dataGridView1.Columns["bedarfMW"].Visible = true;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = true;
                button3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                filterStorage = !filterStorage;
            }
            else
            {
                //dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lagerOrtWerft"].Visible = true;
                //dataGridView1.Columns["auftragsJahr"].Visible = true;
                //dataGridView1.Columns["kostenStelleMW"].Visible = true;
                //dataGridView1.Columns["bedarfMW"].Visible = true;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = false;
                button3.BackColor = Color.Red;
                filterStorage = !filterStorage;
            }
        }

        //
        // Filter alle Engineerspalten
        //
        private void button2_Click(object sender, EventArgs e)
        {
            if (filterEngineer)
            {

                dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = true;
                dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = true;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = true;
                dataGridView1.Columns["lagerOrtWerft"].Visible = true;
                //dataGridView1.Columns["auftragsJahr"].Visible = true;
                //dataGridView1.Columns["kostenStelleMW"].Visible = true;
                //dataGridView1.Columns["bedarfMW"].Visible = true;
                button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
                filterEngineer = !filterEngineer;
            }
            else
            {
                dataGridView1.Columns["bestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellFreigabeDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["bestellungDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userBestellungDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["bestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userbestaetigterLieferterminDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareneingangDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareneingangDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["wareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferSollHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["wareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userWareLieferIstHHDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userlieferungWerftIstDataGridViewTextBoxColumn"].Visible = false;
                dataGridView1.Columns["lieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["userLieferungSchiffSollDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["versandArtWerftDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["packstueckDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["lieferscheinNummerDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["laufendeNummerAuftragDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["auftragsNummerDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["remarksDataGridViewTextBoxColumn"].Visible = false;
                //dataGridView1.Columns["empfangsStelleWerft"].Visible = false;
                dataGridView1.Columns["lagerOrtWerft"].Visible = true;
                //dataGridView1.Columns["auftragsJahr"].Visible = true;
                //dataGridView1.Columns["kostenStelleMW"].Visible = true;
                //dataGridView1.Columns["bedarfMW"].Visible = true;
                button2.BackColor = Color.Red;
                filterEngineer = !filterEngineer;
            }
        }

        //
        // Filter Zeilen 1
        //
        private void button5_Click(object sender, EventArgs e)
        {
            if (filterBereitstellungHHkleinerLieferterminHH)
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}' AND  ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
                button5.BackColor = System.Drawing.Color.Gainsboro;
                filterBereitstellungHHkleinerLieferterminHH = !filterBereitstellungHHkleinerLieferterminHH;
            }
            else
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("bestaetigterLiefertermin > wareLieferSollHH AND wareLieferSollHH > '{4}' AND Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), DateTime.Now);     
                button5.BackColor = Color.Red;
                filterBereitstellungHHkleinerLieferterminHH = !filterBereitstellungHHkleinerLieferterminHH;
            }
        }

        //
        // Filter Zeilen 2
        //
        private void button6_Click(object sender, EventArgs e)
        {
            if (filterBereitstellungWerftkleinerLieferfaehigkeitHH)
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}' AND  ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
                button6.BackColor = System.Drawing.Color.Gainsboro;
                filterBereitstellungWerftkleinerLieferfaehigkeitHH = !filterBereitstellungWerftkleinerLieferfaehigkeitHH;
            }
            else
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("wareLieferIstHH > lieferungSchiffSoll AND wareLieferIstHH > '{4}' AND Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), DateTime.Now);
                button6.BackColor = Color.Red;
                filterBereitstellungWerftkleinerLieferfaehigkeitHH = !filterBereitstellungWerftkleinerLieferfaehigkeitHH;
            }
        }

        //
        // Export to Excel
        //
        private void button7_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }

        //
        // Filter Zeilen 3
        //
        private void button8_Click(object sender, EventArgs e)
        {
            if (filterGelieferteWareHH)
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}' AND  ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
                button8.BackColor = System.Drawing.Color.Gainsboro;
                filterGelieferteWareHH = !filterGelieferteWareHH;
            }
            else
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("wareneingang IS NOT NULL AND Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"));
                button8.BackColor = Color.Red;
                filterGelieferteWareHH = !filterGelieferteWareHH;
            }
        }

        //
        // Filter Zeilen 4
        //
        private void button9_Click(object sender, EventArgs e)
        {
            if (filtergelieferteWareMW)
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}' AND  ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
                button9.BackColor = System.Drawing.Color.Gainsboro;
                filtergelieferteWareMW = !filtergelieferteWareMW;
            }
            else
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("lieferungWerftIst IS NOT NULL AND Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"));
                button9.BackColor = Color.Red;
                filtergelieferteWareMW = !filtergelieferteWareMW;
            }
        }

        //
        // Reload Data
        //
        private void button10_Click(object sender, EventArgs e)
        {
            this.vw_comm_LieferketteTableAdapter.Fill(this.aSCDataSet.vw_comm_Lieferkette);
            dataGridView1.Update();
            
        }

        //
        // Spalten sortieren
        //
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //this.dataGridView1.Sort(this.dataGridView1.Columns["Venue"], ListSortDirection.Ascending);
        }



        private void textBox6_TextChanged(object sender, EventArgs e)
        {

            vwcommLieferketteKaiExportBindingSource.Filter = string.Format("ISNULL(CONVERT(Liefertermin, 'System.String'),'') LIKE '{0}' AND ISNULL(lieferscheinNummer,'') LIKE '{1}'", (textBox7.Text + "*"), (textBox6.Text + "*"));
                
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            vwcommLieferketteKaiExportBindingSource.Filter = string.Format("ISNULL(CONVERT( Liefertermin, 'System.String'),'') LIKE '{0}' AND ISNULL(lieferscheinNummer,'') LIKE '{1}'", (textBox7.Text + "*"), (textBox6.Text + "*"));
            
        }

        //
        // Kai Excel Export
        //
        private void button11_Click(object sender, EventArgs e)
        {
            //dataGridView2.SelectAll();
            //DataObject dataObj = dataGridView2.GetClipboardContent();
            //if (dataObj != null)
            //    Clipboard.SetDataObject(dataObj);
            //Microsoft.Office.Interop.Excel.Application xlexcel;
            //Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            //Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            //object misValue = System.Reflection.Missing.Value;
            //xlexcel = new Microsoft.Office.Interop.Excel.Application();
            //xlexcel.Visible = true;
            //xlWorkBook = xlexcel.Workbooks.Add(misValue);
            //xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            //Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            //CR.Select();
            //xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            //dataGridView2.ClearSelection();
            string pfad = System.Windows.Forms.Application.StartupPath.ToString();
            dataGridView2.SelectAll();
            DataObject dataObj = dataGridView2.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                xlWorkBook = xlexcel.Workbooks.Open((pfad + "\\TemplateKaiExport.xlsx"));
                xlexcel.Visible = true;
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1];
                CR.Select();

                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
                //xlWorkSheet.Cells.Replace("Licht", "917");
                //xlWorkSheet.Cells.Replace("Sound", "939");
                //xlWorkSheet.Cells.Replace("Video", "984");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Konnte Vorlage für Kabelliste nicht finden");
            }
            dataGridView1.ClearSelection();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (filterBestelltAberNichtGeliefert)
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}' AND JLM LIKE '{3}' AND  ISNULL(Positions_ID,'') LIKE '{4}' ", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"), (textBox4.Text + "*"), (textBox5.Text + "*"));
                button12.BackColor = System.Drawing.Color.Gainsboro;
                filterBestelltAberNichtGeliefert = !filterBestelltAberNichtGeliefert;
            }
            else
            {
                vwcommLieferketteBindingSource1.Filter = string.Format("LieferungWerftIst IS NULL AND lieferungSchiffSoll IS NOT NULL AND lieferungSchiffSoll < '{0}' ", DateTime.Now);
                button12.BackColor = Color.Red;
                filterBestelltAberNichtGeliefert = !filterBestelltAberNichtGeliefert;
            }
        }

        private void label6_Click(object sender, EventArgs e) { }

        
        

        
    }
}
